/**
 * Title: Hyperbolic engine
 * Description: jFamilyTree Engine
 * Version: provider
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 * Update: Mon Mar 10 00:00:00 CEST 2008
 */
package jFamilyTree.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import jFamilyTree.model.Settings;

/**
 * Status bar
 * 
 * @author Bernard Bou
 */
public class Statusbar extends JToolBar
{
	private static final long serialVersionUID = 1L;

	/**
	 * Put type
	 */
	public static enum PutType
	{
		INFO, LINK
	}

	/**
	 * Content panel increment
	 */
	static private final int theContentIncrement = 20;

	/**
	 * Content panel increment
	 */
	static private final int theContentMinimum = 20;

	/**
	 * Content panel increment
	 */
	static private final int theContentMaximum = 200;

	// D A T A

	// component
	/**
	 * Label
	 */
	private JTextField theLabelTextField;

	/**
	 * Input
	 */
	private JTextField theInputTextField;

	/**
	 * Content
	 */
	private JTextPane theContentEditor;

	/**
	 * Content pane
	 */
	private JComponent theContentPane;

	/**
	 * Ouput icon label
	 */
	private JLabel theOutputIconLabel;

	/**
	 * Input icon label
	 */
	private JLabel theInputIconLabel;

	/**
	 * Toggle button
	 */
	private JToggleButton theToggleButton;

	/**
	 * Edit box
	 */
	private Box theEditBox;

	// images
	/**
	 * Images
	 */
	static private ImageIcon[] theImages = new ImageIcon[] { //
	new ImageIcon(Statusbar.class.getResource("images/info.png")), //
			new ImageIcon(Statusbar.class.getResource("images/linking.png")), //
	};

	// colors
	/**
	 * Label background color
	 */
	static private Color[] theBackColor = { Color.WHITE, new Color(0, 0, 128) };

	/**
	 * Label foreground color
	 */
	static private Color[] theForeColor = { Color.BLACK, Color.WHITE };

	// font
	/**
	 * Label font
	 */
	private Font theLabelFont;

	// style
	static private SimpleAttributeSet theContentStyle = new SimpleAttributeSet();
	static
	{
		StyleConstants.setFontFamily(theContentStyle, "Dialog");
		StyleConstants.setFontSize(theContentStyle, 10);
	}

	// C O N S T R U C T O R

	/**
	 * Constructor
	 */
	public Statusbar()
	{
		super(HORIZONTAL);
		setLayout(new GridBagLayout());
		setFloatable(true);
		init(null);
	}

	/**
	 * Init
	 * 
	 * @param theseSettings
	 *        settings
	 */
	public void init(Settings theseSettings)
	{
		// colors
		if (theseSettings != null)
		{
			if (theseSettings.theBackColor != null)
			{
				theBackColor[PutType.INFO.ordinal()] = theseSettings.theBackColor.brighter();
				theBackColor[PutType.LINK.ordinal()] = theseSettings.theBackColor.darker();
			}
			if (theseSettings.theForeColor != null)
			{
				theForeColor[PutType.INFO.ordinal()] = theseSettings.theForeColor.darker();
				theForeColor[PutType.LINK.ordinal()] = theseSettings.theForeColor.brighter();
			}
		}

		// font
		String thisFontFace = (theseSettings == null || theseSettings.theFontFace == null || theseSettings.theFontFace.isEmpty()) ? "Dialog" : theseSettings.theFontFace;
		int thisFontSize = theseSettings == null ? 12 : theseSettings.theFontSize;
		if (thisFontSize > 18)
			thisFontSize = 18;
		if (thisFontSize < 14)
			thisFontSize = 12;
		theLabelFont = new Font(thisFontFace, Font.PLAIN, thisFontSize);

		// toggle button
		theToggleButton = new JToggleButton();
		theToggleButton.setIcon(new ImageIcon(Statusbar.class.getResource("images/editstart.png")));
		theToggleButton.setSelectedIcon(new ImageIcon(Statusbar.class.getResource("images/editstop.png")));
		theToggleButton.setOpaque(true);
		theToggleButton.setBorder(null);
		theToggleButton.setToolTipText("Locks status bar and switches to edit mode");
		theToggleButton.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent e)
			{
				boolean isSelected = e.getStateChange() == ItemEvent.SELECTED;
				if (isSelected)
				{
					theEditBox.add(theInputIconLabel);
					theEditBox.add(theInputTextField);
				}
				else
				{
					theEditBox.remove(theInputIconLabel);
					theEditBox.remove(theInputTextField);
				}
				theEditBox.validate();
				theOutputIconLabel.setIcon(theImages[PutType.INFO.ordinal()]);
			}
		});

		// enlarge/shrink buttons
		JButton thisMoreButton = new JButton();
		thisMoreButton.setIcon(new ImageIcon(Statusbar.class.getResource("images/plus.png")));
		thisMoreButton.setBorder(null);
		thisMoreButton.setContentAreaFilled(false);
		thisMoreButton.setFocusable(false);
		thisMoreButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		thisMoreButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Dimension thisSize = theContentPane.getPreferredSize();
				int h = thisSize.height + theContentIncrement;
				if (h <= theContentMaximum)
				{
					thisSize.height = h;
					theContentPane.setPreferredSize(thisSize);
					getParent().validate();
					getParent().repaint();
				}
			}
		});
		JButton thisLessButton = new JButton();
		thisLessButton.setIcon(new ImageIcon(Statusbar.class.getResource("images/minus.png")));
		thisLessButton.setBorder(null);
		thisLessButton.setContentAreaFilled(false);
		thisLessButton.setFocusable(false);
		thisLessButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		thisLessButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Dimension thisSize = theContentPane.getPreferredSize();
				int h = thisSize.height - theContentIncrement;
				if (h >= theContentMinimum)
				{
					thisSize.height = h;
					theContentPane.setPreferredSize(thisSize);
					getParent().validate();
					getParent().repaint();
				}
			}
		});

		// button box
		Box thisButtonBox = new Box(BoxLayout.Y_AXIS);
		thisButtonBox.add(thisMoreButton);
		thisButtonBox.add(thisLessButton);

		// output
		theOutputIconLabel = new JLabel();
		theOutputIconLabel.setIcon(theImages[PutType.INFO.ordinal()]);
		theLabelTextField = new JTextField();
		theLabelTextField.setEditable(false);
		theLabelTextField.setBackground(Color.WHITE);
		theLabelTextField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		theLabelTextField.setFont(theLabelFont);
		theLabelTextField.setToolTipText("Displays node information");

		// input
		theInputIconLabel = new JLabel();
		theInputIconLabel.setIcon(new ImageIcon(Statusbar.class.getResource("images/edit.png")));
		theInputTextField = new JTextField();
		theInputTextField.setEditable(true);
		theInputTextField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		theInputTextField.setToolTipText("Input information");

		// content
		theContentEditor = new JTextPane();
		theContentEditor.setContentType("text/html; charset=UTF-8");
		theContentEditor.setEditable(false);
		theContentEditor.setBorder(null);
		theContentEditor.setFont(new Font("Dialog", Font.PLAIN, 8));
		theContentEditor.setToolTipText("Displays node content");
		theContentEditor.setPreferredSize(new Dimension(400, 30));

		theContentPane = new JScrollPane(theContentEditor);
		theContentPane.setBorder(BorderFactory.createLineBorder(Color.GRAY));

		// edit box
		theEditBox = new Box(BoxLayout.X_AXIS);
		theEditBox.add(theLabelTextField);

		// assemble
		add(theOutputIconLabel, new GridBagConstraints(0, 0, 1, 2, 0.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 10), 0, 0));
		add(theEditBox, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 0, 0));
		add(theContentPane, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 5, 0), 0, 0));
		add(theToggleButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
		add(thisButtonBox, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
	}

	/**
	 * Set colors
	 * 
	 * @param thisType
	 *        information type
	 */
	private void setColors(PutType thisType)
	{
		Color thisBackColor = theBackColor[thisType.ordinal()];
		setBackground(thisBackColor);

		theEditBox.setBackground(thisBackColor);
		theLabelTextField.setBackground(thisBackColor);
		theInputTextField.setBackground(thisBackColor);
		theContentEditor.setBackground(thisBackColor);

		Color thisForeColor = theForeColor[thisType.ordinal()];
		setForeground(thisForeColor);

		theEditBox.setForeground(thisForeColor);
		theLabelTextField.setForeground(thisForeColor);
		theInputTextField.setForeground(thisForeColor);
		theContentEditor.setForeground(thisForeColor);

		StyleConstants.setForeground(theContentStyle, thisForeColor);
	}

	/**
	 * Put status
	 * 
	 * @param thisLabel
	 *        label
	 * @param thisContent
	 *        content
	 * @param thisType
	 *        status type as per below
	 */
	public void put(String thisLabel, String thisContent, PutType thisType)
	{
		// icon and colors
		theOutputIconLabel.setIcon(theImages[thisType.ordinal()]);
		setColors(thisType);

		// label
		theLabelTextField.setText(thisLabel);

		// content
		theContentEditor.setText(thisContent);
		theContentEditor.setCaretPosition(0);

		// style
		StyledDocument thisStyledDocument = theContentEditor.getStyledDocument();
		thisStyledDocument.setCharacterAttributes(0, thisStyledDocument.getLength(), theContentStyle, false);
	}

	/**
	 * Put status
	 * 
	 * @param thisMessage
	 *        content
	 */
	public void put(String thisMessage)
	{
		theContentEditor.setText(thisMessage);
		theContentEditor.setCaretPosition(0);
	}

	/**
	 * Get input
	 * 
	 * @return input
	 */
	public String get()
	{
		return this.theInputTextField.getText();
	}
}
